package cn.itcast.service.impl;

import cn.itcast.dao.OrderListDao;
import cn.itcast.dao.SetmealDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderListService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListDao orderListDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public PageResult queryOrderList(Integer currentPage, Integer pageSize,
                                     Integer total, String queryString,
                                     Date minTime, Date maxTime,
                                     String orderType, String orderStatus,
                                     Integer payStatus) {

        PageResult pageResult = new PageResult(null, null);
//        System.out.println(currentPage+pageSize);
        PageHelper.startPage(currentPage, pageSize);

        List<Order> all = orderListDao.findAll(queryString, minTime, maxTime, orderType, orderStatus, payStatus);
        //填充每个预约信息的预约套餐信息
        for (Order order : all) {
            if (order.getSetmealId()!=null){
                order.setSetmeal(setmealDao.findById(order.getSetmealId()));
            }
        }
        if (all==null)
        {
            log.info("orderlist没有返回值============================");
            return pageResult;
        }


        log.info("orderlist数据为"+all);
        PageInfo pageInfo = new PageInfo(all);

        ArrayList rows = new ArrayList();
        for (Object order : pageInfo.getList()) {
            rows.add(order);
        }
        pageResult.setRows(rows);
        pageResult.setTotal(pageInfo.getTotal());
        log.info("pageresult的数据为"+pageResult);

        return pageResult;
    }

    public Order queryOrderById(String id){

        Order order = orderListDao.findById(id);
        if (order==null){
            log.error("数据库没有此条数据,id为"+id);
            return null;
        }
        return order;
    }

    @Override
    public boolean saveOrder(Order order) {
        if (order.getId()==null){
            String s = UUID.randomUUID().toString();
            //添加id
            order.setId(s);
            return addOrder(order);
        }else {
            return updateOrder(order);
        }
    }

    public boolean addOrder(Order order) {
        //后台系统新增默认为电话预约支付状态默认为未支付
        if (order.getOrderType()==null){
            order.setOrderType("电话预约");
        }
        if (order.getPayStatus()==null){
            order.setPayStatus(0);
        }
        //判断预约日期(不能为今日之前或当前)
        Date today = new Date();
        Date orderDate = order.getOrderDate();
        int compareDate = compareDate(today, orderDate);
        System.out.println("两个时间分别为"+today+"==="+orderDate+"且比较结果为"+compareDate);
        if (compareDate>0){
            log.error("预约时间错误,预约时间不能在当天或当天前"+orderDate);
            return false;
        }
        boolean flag = orderListDao.add(order);
        log.info("添加状态为"+flag);
        return flag;
    }

    public boolean updateOrder(Order order) {
        if (order.getOrderType()==null){
            order.setOrderType("电话预约");
        }
        if (order.getPayStatus()==null){
            order.setPayStatus(0);
        }
        //判断预约日期(不能为今日之前或当前)
        Date today = new Date();
        Date orderDate = order.getOrderDate();
        int compareDate = compareDate(today, orderDate);
        System.out.println("两个时间分别为"+today+"==="+orderDate+"且比较结果为"+compareDate);
        if (compareDate>0){
            log.error("预约时间错误,预约时间不能在当天或当天前"+orderDate);
            return false;
        }
        boolean flag = orderListDao.update(order);
        log.info("修改状态为"+flag);
        return flag;
    }



    private static int compareDate(Date date1,Date date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date1).compareTo(sdf.format(date2));
    }
}
